package cas.ihep.spark;

import cas.ihep.jni.HepEventNativeInterface;

import java.io.*;

/**
 * Created by gesturewei on 16-10-4.
 */
public class HepEvent implements java.io.Externalizable{

    private static final long serialVersionUID = 1L;
    protected int bufferId;

    public HepEvent(){
        StringBuilder sb=new StringBuilder();
        sb.append("sparkhep-");
        sb.append(HepEventNativeInterface.getpid());
        sb.append('-');
        sb.append(Long.toHexString(System.currentTimeMillis()));
        sb.append('-');
        sb.append(Integer.toHexString(hashCode()));
        bufferId=HepEventNativeInterface.create0(0,sb.toString(),1,8192,0100);
        //System.err.println("Default constructor for creating");
    }

    public HepEvent(String name){
        bufferId=HepEventNativeInterface.create0(0,name,1,-1,0);
        //System.err.println("One string parameter for opening");
    }

    public void dispose(){
        if (bufferId>0) {
            HepEventNativeInterface.dispose0(bufferId);
            bufferId=-1;
        }
    }

    public void unlink(){
        if (bufferId>0){
            HepEventNativeInterface.unlink0(bufferId);
        }
    }

    protected void finalize()throws Throwable{
        dispose();
        super.finalize();
    }

    public int getPhysicalSize() {
        return HepEventNativeInterface.nativeGetPhysicalSize(bufferId);
    }

    public int getPackedSize() {
        return HepEventNativeInterface.nativeGetPackedSize(bufferId);
    }

    public short getNumberOfEvents() {
        return HepEventNativeInterface.nativeGetNumberOfEvents(bufferId);
    }

    public short getPackingAlgorithm() {
        return HepEventNativeInterface.nativeGetPackingAlgorithm(bufferId);
    }

    public int getVersion() {
        return HepEventNativeInterface.nativeGetVersion(bufferId);
    }

    public int getEventData(DataOutput ostrm) {
        try{
            return HepEventNativeInterface.nativeGetEventData0(bufferId,ostrm);
        }catch(java.io.IOException e){
            e.printStackTrace();
            return -1;
        }
    }

    public void setPhysicalSize(int physize_) {
        HepEventNativeInterface.nativeSetPhysicalSize(bufferId,physize_);
    }

    public void setPackedSize(int packedsize_) {
        HepEventNativeInterface.nativeSetPackedSize(bufferId,packedsize_);
    }

    public void setNumberOfEvents(short count) {
        HepEventNativeInterface.nativeSetNumberOfEvents(bufferId,count);
    }

    public void setPackingAlgorithm(short alg) {
        HepEventNativeInterface.nativeSetPackingAlgorithm(bufferId,alg);
    }

    public void setVersion(int ver) {
        HepEventNativeInterface.nativeSetVersion(bufferId,ver);
    }

    public int setEventData(DataInput src) {
        try{
            return HepEventNativeInterface.nativeSetEventData0(bufferId,src);
        }catch(java.io.IOException e){
            return -1;
        }
    }

    public void writeExternal(ObjectOutput out)throws IOException{
        HepEventNativeInterface.nativeGetObjectData0(bufferId,out);
    }

    public void readExternal(ObjectInput in)throws IOException,ClassNotFoundException{
        HepEventNativeInterface.nativeSetObjectData0(bufferId,in);
    }
}

